home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 July / EnigmA AMIGA RUN 09 (1996)(G.R. Edizioni)(IT)[!][issue 1996-07 & 08][EARSAN CD VIII].iso / earcd / util3 / fiflb382.lha / testfifo.c < prev    next >
C/C++ Source or Header  |  1995-12-13  |  3KB  |  166 lines

  1.  
  2. /*
  3.  *  TEST.C
  4.  *
  5.  *  TEST [R/W][N] fifo_name
  6.  *
  7.  *  test fifo operation
  8.  *
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <libraries/dos.h>
  14. #if defined(_DCC)    /*  at least the very old DICE 2.06 */
  15. #include <clib/exec_protos.h>
  16. #else
  17. #include <proto/exec.h>
  18. #endif
  19. #include <clib/alib_protos.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <fcntl.h>        /*  read(), write() */
  24. #if defined(__GNUC__)
  25. #include <unistd.h>        /*  read(), write() */
  26. #endif
  27. #include <libraries/fifo.h>
  28. #include <proto/fifo.h>
  29.  
  30. typedef struct MsgPort    MsgPort;
  31. typedef struct Message    Message;
  32.  
  33. struct Library *FifoBase;
  34. void *Fh;
  35. MsgPort *WaPort;
  36. char IBuf[256];
  37.  
  38. void DoWait(long);
  39. void myexit(void);
  40.  
  41. int main(ac, av)
  42. int ac;
  43. char *av[];
  44. {
  45.     if (ac != 3) {
  46.     fputs("TEST [R/W][N] fifo_name\n", stderr);
  47.     exit(1);
  48.     }
  49.     atexit(myexit);
  50.  
  51.     WaPort = CreatePort(NULL, 0);
  52.  
  53.     if ((FifoBase = OpenLibrary(FIFONAME, 0))) {
  54.     long flags = FIFOF_NORMAL;
  55.  
  56.     fputs("fifo.library opened!\n", stderr);
  57.  
  58.     if (av[1][1] == 'N')
  59.         flags |= FIFOF_NBIO;
  60.  
  61.     switch(av[1][0]) {
  62.     case 'R':
  63.         if ((Fh = OpenFifo(av[2], 4096, FIFOF_READ | flags))) {
  64.         long i = 0;
  65.         char *ptr;
  66.  
  67.         fprintf(stderr, "fifo is %ld bytes\n", BufSizeFifo(Fh));
  68.         for (;;) {
  69.             long n = ReadFifo(Fh, &ptr, i);
  70.             if (n > 64)     /*  limit amount of data read/loop */
  71.             n = 64;
  72.             i = n;
  73.  
  74.             if (n < 0) {
  75.             fputs("EOF\n", stderr);
  76.             break;
  77.             }
  78.             if (n > 0) {
  79.             /*
  80.              *  just so other windows doesn't freeze while we
  81.              *  output kilobytes.
  82.              */
  83.  
  84.             write(1, ptr, n);
  85.             } else {        /*    n == 0    */
  86.             if (flags & FIFOF_NBIO) {
  87.                 DoWait(FREQ_RPEND);
  88.             } else {
  89.                 puts("0 bytes avail?");
  90.             }
  91.             }
  92. #if defined(_DCC)
  93.             chkabort();    /*  exit if ^C */
  94. #endif
  95.         }
  96.         }
  97.         break;
  98.     case 'W':
  99.         if ((Fh = OpenFifo(av[2], 4096, FIFOF_WRITE | FIFOF_KEEPIFD | flags))) {
  100.         long n;
  101.  
  102.         fprintf(stderr, "fifo is %ld bytes\n", BufSizeFifo(Fh));
  103.  
  104.         while ((n = read(0, IBuf, sizeof(IBuf))) > 0) {
  105.             long r;
  106.  
  107. loop:
  108.             r = WriteFifo(Fh, IBuf, n);
  109.             if (r != n) {
  110.             if (r >= 0 && (flags & FIFOF_NBIO)) {
  111.                 DoWait(FREQ_WAVAIL);
  112.                 goto loop;
  113.             } else {
  114.                 fprintf(stderr, "write failed! %ld\n", r);
  115.                 break;
  116.             }
  117.             }
  118.         }
  119.         }
  120.         break;
  121.     default:
  122.         fputs("bad command line", stderr);
  123.         break;
  124.     }
  125.     }
  126.     return(0);
  127. }
  128.  
  129. void
  130. myexit(void)
  131. {
  132.     if (Fh) {
  133.     CloseFifo(Fh, FIFOF_EOF);
  134.     Fh = 0;
  135.     }
  136.     if (FifoBase) {
  137.     CloseLibrary(FifoBase);
  138.     FifoBase = 0;
  139.     }
  140.     if (WaPort) {
  141.     DeletePort(WaPort);
  142.     WaPort = NULL;
  143.     }
  144. }
  145.  
  146. void
  147. DoWait(req)
  148. long req;
  149. {
  150.     Message msg;
  151.     long mask = 0;
  152.  
  153.     fputs("WAIT\n", stderr);
  154.     msg.mn_ReplyPort = WaPort;
  155.     RequestFifo(Fh, &msg, req);
  156.  
  157.     while (msg.mn_Node.ln_Type == NT_MESSAGE) {
  158.     mask = Wait(SIGBREAKF_CTRL_C | (1 << WaPort->mp_SigBit));
  159.     if (mask & SIGBREAKF_CTRL_C)
  160.         RequestFifo(Fh, &msg, FREQ_ABORT);
  161.     }
  162.     GetMsg(WaPort);
  163.     if (mask & SIGBREAKF_CTRL_C)
  164.     fputs("WAIT: ABORT\n", stderr);
  165. }
  166.